<?php
/**
 * helper mailer is a simple send mail class
 * 	version:	0.1
 * 	update:		2009/7/28
 *
 */
class Helper_Mailer {
	public $phpmailer;
	
	public function __construct() {
		Mid::loadLib ( 'phpmailer:class.phpmailer' );
		$this->phpmailer = new phpmailer ( );
		$phpmailer_dir = str_replace ( '.php', DIRECTORY_SEPARATOR, Mid::path ( '$:lib:phpmailer' ) );
		$this->phpmailer->PluginDir = $phpmailer_dir;
	}
	
	/**
	 * object phpmailer handle
	 *
	 * @return object phpmailer
	 */
	public function phpmailerInstance($new = FALSE) {
		if ($new === TRUE) {
			$this->phpmailer = new PHPMailer ( );
		}
		return $this->phpmailer;
	}
	
	/**
	 * create a mailer handle
	 *
	 * @param string $type (smtp|sendmail|mail)
	 * @param string $host (like:smtp.sina.com:443) 
	 * @param string $mail (like:corrie<corrie@sina.com>)
	 * @param string $user
	 * @param string $pswd
	 */
	public function login($type, $mail, $host = NULL, $user = NULL, $pswd = NULL) {
		switch ($type) {
			case 'smtp' :
				$this->phpmailer->IsSMTP ();
				break;
			case 'sendmail' :
				$this->phpmailer->IsSendmail ();
				break;
			case 'mail' :
				$this->phpmailer->IsMail ();
				break;
		}
		
		if ($mail) {
			$match = $this->parseAddress ( $mail );
			$this->phpmailer->From = $match [0];
			$this->phpmailer->FromName = $match [1];
		}
		
		if ($host === NULL) {
			$host = 'localhost';
		}
		$this->phpmailer->Host = $host;
		
		if ($user !== NULL || $pswd !== NULL) {
			if ($type == 'smtp') {
				$this->phpmailer->SMTPAuth = true;
			}
			$this->phpmailer->Username = $user;
			$this->phpmailer->Password = $pswd;
		}
		return true;
	}
	
	/**
	 * create a mail
	 *
	 * @param string $type (like:TXT|HTML)
	 * @param string $subject mail subject
	 * @param string $body mail body
	 * @param string $attach mail attachment
	 */
	public function write($subject, $body, $attach = NULL, $type = 'HTML', $charset = 'utf-8', $encode = 'base64') {
		$this->phpmailer->Subject = $subject;
		$this->phpmailer->Body = $body;
		if ($attach !== NULL && ! empty ( $attach )) {
			foreach ( $attach as /*$k =>*/ $v ) {
				/*if (isset ( $k ) && is_string ( $k )) {
					$this->phpmailer->AddAttachment ( $v, $k );
				} else {*/
					$this->phpmailer->AddAttachment ( $v );
				//}
			}
		}
		if ($type != 'HTML') {
			$this->phpmailer->IsHTML ( false );
		} else {
			$this->phpmailer->IsHTML ( true );
		}
		$this->phpmailer->CharSet = $charset;
		$this->phpmailer->Encoding = $encode;
		return true;
	}
	
	/**
	 * Send to somebody something
	 *
	 * @param mixed $to
	 * @param mixed $cc
	 * @param mixed $bcc
	 * @param mixed $rt
	 */
	public function sendto($to, $cc = NULL, $bcc = NULL, $rt = NULL) {
		if (is_array ( $to ) && ! empty ( $to )) {
			foreach ( $to as $address ) {
				$match = $this->parseAddress ( $address );
				if($match [1]!=''){
					$this->phpmailer->AddAddress ( $match [0], $match [1] );
				}else{
					$this->phpmailer->AddAddress ( $match [0]);
				}
			}
		} elseif ($to !== NULL) {
			$match = $this->parseAddress ( $to );		
			if($match [1]!=''){
				$this->phpmailer->AddAddress ( $match [0], $match [1] );
			}else{
				$this->phpmailer->AddAddress ( $match [0]);
			}
		}
		
		if (isset ( $cc ) &&  is_array ( $cc ) && ! empty ( $cc )) {
			foreach ( $cc as $address ) {
				$match = $this->parseAddress ( $address );		
				if($match [1]!=''){
					$this->phpmailer->AddCC ( $match [0], $match [1] );
				}else{
					$this->phpmailer->AddCC ( $match [0]);
				}
			}
		} elseif ($cc !== NULL) {
			$match = $this->parseAddress ( $cc );				
			if($match [1]!=''){
				$this->phpmailer->AddCC ( $match [0], $match [1] );
			}else{
				$this->phpmailer->AddCC ( $match [0]);
			}
		}
		
		if (isset ( $bcc ) && $bcc !== NULL && is_array ( $bcc ) && ! empty ( $bcc )) {
			foreach ( $bcc as $address ) {
				$match = $this->parseAddress ( $address );					
				if($match [1]!=''){
					$this->phpmailer->AddBCC ( $match [0], $match [1] );
				}else{
					$this->phpmailer->AddBCC ( $match [0]);
				}
			}
		} elseif ($bcc !== NULL) {
			$match = $this->parseAddress ( $bcc );					
			if($match [1]!=''){
				$this->phpmailer->AddBCC ( $match [0], $match [1] );
			}else{
				$this->phpmailer->AddBCC ( $match [0]);
			}
		}
		
		if (isset ( $rt ) && is_array ( $rt ) && ! empty ( $rt )) {
			foreach ( $rt as $address ) {
				$match = $this->parseAddress ( $address );					
				if($match [1]!=''){
					$this->phpmailer->AddReplyTo ( $match [0], $match [1] );
				}else{
					$this->phpmailer->AddReplyTo ( $match [0]);
				}
			}
		} elseif ($rt !== NULL) {
			$match = $this->parseAddress ( $rt );
			if($match [1]!=''){
				$this->phpmailer->AddReplyTo ( $match [0], $match [1] );
			}else{
				$this->phpmailer->AddReplyTo ( $match [0]);
			}
		}
		$this->send ();
	}	
	
	/**
	 * Quick send mail
	 *
	 * @param mixed $to
	 * @param string $subject
	 * @param string $body
	 */
	public function quickSend($to, $subject, $body) {
		$this->write ( $subject, $body );
		$this->sendto ( $to );
	}
	
	/**
	 * Send mail to single address
	 *
	 * @param mixed $to
	 * @param string $subject
	 * @param string $body
	 */
	public function singleSend($to, $subject, $body) {
		$this->write ( $subject, $body );
		if($this->phpmailer->Mailer){
			$this->phpmailer->SMTPKeepAlive = TRUE;
		}
		if (is_array ( $to ) && ! empty ( $to )) {
			foreach ( $to as $address ) {
				$this->sendto ( $address );
				$this->clear();
			}
		} elseif ($to !== NULL) {
			$this->sendto ( $to );
		}
	}
	
	/**
	 * Send mail
	 *
	 */
	private function send() {
		$this->phpmailer->Send ();
	}
	
	/**
	 * Clear all address
	 *
	 */
	private function clear() {
		$this->phpmailer->ClearAllRecipients ();
	}
	
	/**
	 * Reset phpmailer object
	 *
	 */
	private function reset() {
		$this->phpmailer ( TRUE );
	}
	
	/**
	 * Parse String to a address and a name 
	 *
	 * @param string $str
	 * @return array
	 */
	private function parseAddress($str) {
		if (preg_match ( '/(.*)<(.*)>/', $str, $match )) {
			$address = $match [2];
			$name = $match [1] != '' ? $match [1] : '';
		} else {
			$address = $str;
			$name = '';
		}
		//exit($name . $address);
		return array ($address, $name );
	}
}
?>